package com.srzk.youthleagueback.controller;

import com.srzk.youthleagueback.pojo.Note;
import com.srzk.youthleagueback.results.CodeMsg;
import com.srzk.youthleagueback.results.Result;
import com.srzk.youthleagueback.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@CrossOrigin
@RestController
@RequestMapping("/part4")
public class NoteController {

    @Autowired
    NoteService noteService ;
    @PostMapping("/note")
    public Result<String> addNote(@RequestParam("user")String user,@RequestParam("year")String year,@RequestParam("content")String content){
        Note note = new Note(user, year, content);
        boolean flag;
        if(noteService.searchNoteByUserAndYear(user,year)==null){
            flag = noteService.addNote(note);
            if (flag)
                return Result.success("添加成功");
            else
                return Result.error(SERVER_ERROR);
        }else{
            flag = noteService.updateNote(note);
            if (flag)
                return Result.success("更新成功");
            else
                return Result.error(SERVER_ERROR);
        }
    }
    @GetMapping("/note")
    public Result<Map<String, Object>>getNote(@RequestParam("user")String user, @RequestParam("year")String year){
        Note note = noteService.searchNoteByUserAndYear(user,year);
        if(note==null){
            return Result.error(NOT_EXIST);
        }else {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("content",note.getContent());
            return Result.success(map);
        }
    }


}
